<template>
    <el-card style="max-width: 99%">
      <template #header>
        <div class="card-header">
          <span>机票管理</span>
          <el-button type="primary" style="height: 36px;float: right" @click="openTc(1)">添加机票</el-button>
        </div>
      </template>
      <el-row style="margin-bottom: 20px">
        <el-col :span="9" style="display:flex;">
          <el-input v-model="originName" placeholder="出发地" class="i1" />
          <span style="color:#E4E7ED">——</span>
          <el-input v-model="destinationName" placeholder="目的地" class="i2" />
        </el-col>
        <el-col :span="10">
          <el-button type="primary" @click="cx()">搜索</el-button>
          <el-button @click="cz()">重置</el-button>
          <a :href="generateUrl" style="margin-left: 10px"><el-button type="success">导出Excel</el-button></a>
        </el-col>
      </el-row>
      <el-table :data="list" border style="margin-bottom: 15px">
        <el-table-column prop="originName" label="出发地" width="180" />
        <el-table-column prop="destinationName" label="目的地" width="180" />
        <el-table-column prop="flight" label="飞机编号" width="180" />
        <el-table-column prop="seatLeft" label="剩余座位" />
        <el-table-column prop="price" label="价格" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button :icon="Edit" circle @click="openTc(2,scope.row.id)"/>
<!--            <el-button :icon="SemiSelect" circle  @click="open(scope.row.id)"/>-->
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 0 auto;width: 300px">
        <el-pagination @current-change="handleCurrentChange"
            size="small" background layout="prev, pager, next" :total="count" :page-size=pageSize class="mt-4"/>
      </div>
      <div style="margin-left: 25px">
        <input type="file" ref="fileInput" style="display: none" @change="handleFileChange">
        <el-button @click="openFileDialog" type="primary">批量导入信息</el-button>
        <el-button @click="uploadFile" :disabled="!selectedFile" type="success">确定上传</el-button>
        <span>&nbsp;&nbsp;&nbsp;</span>
        <a href="http://localhost:8888/exportExcelTemplate"><el-button type="danger">下载导入模板</el-button></a>
      </div>
    </el-card>
  <el-dialog v-model="dialogFormVisible" :before-close="qx" :title="this.type==1?'添加机票':'编辑'" width="500">
    <el-form :model="aircraftForm" style="margin-right: 60px;margin-left: 60px">
      <el-form-item label="价格" prop="price">
        <el-input v-model="aircraftForm.price" autocomplete="off" />
      </el-form-item>
      <el-form-item label="出发地" prop="origin">
        <el-select v-model="aircraftForm.origin" placeholder="--请选择--">
          <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-select v-model="aircraftForm.destination" placeholder="--请选择--">
          <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="飞机编号" prop="flight">
        <el-input v-model="aircraftForm.flight" autocomplete="off" />
      </el-form-item>
      <el-form-item label="剩余座位" prop="seatLeft">
        <el-input v-model="aircraftForm.seatLeft" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qx()">取消</el-button>
        <el-button type="primary" @click="qd()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios';
import {selectAllAircraft, selectProvince} from "@/api/vue3.js"
import {deleteAircraft} from "@/api/vue3.js"
import {addAircraft} from "@/api/vue3.js"
import {updateAircraft} from "@/api/vue3.js"
import {selectById} from "@/api/vue3.js"
import {Edit, SemiSelect} from "@element-plus/icons-vue";
export default {
  name: 'Aircraft',
  computed: {
    SemiSelect() {
      return SemiSelect
    },
    Edit() {
      return Edit
    },
    generateUrl(){
      const baseUrl='http://localhost:8888/wzz/exportEasyExcel'
      const encodeParam1 = encodeURIComponent(this.originName)
      const encodeParam2 = encodeURIComponent(this.destinationName)
      return `${baseUrl}?originName=${encodeParam1}&destinationName=${encodeParam2}`
    },
  },
  data() {
    return{
      dialogFormVisible: false,//对话框显示
      type:0,//对话框类型
      originName:'',//出发地
      destinationName:'',//目的地
      pageNum:1,//当前页码
      pageSize:5,//每页显示条数
      count:0,//总条数
      list:[],//飞机列表
      selectedFile: null,//上传的文件
      aircraftForm:{},
      areaList:[],//地域列表
      cid:'',//地域id
    }
  },
  mounted() {
    this.selList()
    this.dq()
  },
  methods: {
    dq(){
      selectProvince(this.cid).then(res=>{
        console.log(res)
        this.areaList=res.data.data
      })
    },
    cx() {//查询
      this.origin
      this.destination
      this.selList()
    },
    cz() {//重置
      this.originName = ''
      this.destinationName = ''
      this.selList()
    },
    selList() {
      selectAllAircraft(this.originName, this.destinationName, this.pageNum, this.pageSize).then(res => {
        this.list = res.data.data.list
        this.count = res.data.data.count
      })
    },
    handleCurrentChange(val) {//当前页码改变的事件
      this.pageNum = val;
      this.selList()
    },
    //打开弹窗
    openTc(type,id) {
      this.type = type
      this.dialogFormVisible = true
      if (this.type==2){//根据id查看信息
        selectById(id).then(res=>{
          this.aircraftForm= res.data.data.data
        })
      }
    },
    openFileDialog() {//打开文件选择对话框
      this.$refs.fileInput.click();
    },
    handleFileChange(e) {//文件选择改变事件
      this.selectedFile = e.target.files[0];
    },
    uploadFile() {//上传文件
      if (this.selectedFile) {
        const formData = new FormData();
        formData.append('file', this.selectedFile);
        axios.post('http://localhost:8888/wzz/uploadExcel', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
            .then(response => {
              if (response.data.code == 200) {
                this.$message.success("上传文件成功")
                this.clearFileInput()
                this.selectAllUsers()
              } else {
                this.$message.error(response.data.msg)
                this.clearFileInput()
              }
            })
            .catch(error => {
              this.clearFileInput()
            });
      }
    },
    open(id) {
      ElMessageBox.confirm(
          '是否删除该条信息',
          '消息提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {//删除
            deleteAircraft(id).then((res) => {
              if (res.data.data.count==200){
                ElMessage({
                  type: 'success',
                  message: '删除成功',
                })
                this.selList()
              }else {
                ElMessage({
                  type: 'error',
                  message: '删除失败',
                })
              }
            })
          })
          .catch(() => {//取消
            ElMessage({
              type: 'info',
              message: '删除已取消',
            })
          })
    },
    qd(){
      if (this.type==1){
        addAircraft(this.aircraftForm).then((res) => {
          if(res.data.data.count==200){
            ElMessage({
              type: 'success',
              message: '添加成功',
            })
            this.aircraftForm={}
            this.dialogFormVisible=false
            this.selList()
          }else {
            this.dialogFormVisible=false
            ElMessage({
              type: 'error',
              message: '添加失败',
            })
          }
        })
      }
      if (this.type==2){
        updateAircraft(this.aircraftForm).then((res) => {
          console.log(res)
          if (res.data.data.count == 200) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            this.dialogFormVisible = false
            this.selList()
          } else {
            this.dialogFormVisible = false
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
      }
    },
    qx(){//取消
      this.aircraftForm={}
      this.dialogFormVisible=false
    }
  }
}
</script>

<style scoped>
.el-card{
  width: 100%;
  height: 600px;
  background-color: white;
  margin-top: 20px;
  border-radius: 30px;
}
.el-table{
  margin: 0 auto;
  width: 96%;
}
.i1{
  margin-left: 25px;
  margin-right: 10px;
}
.i2{
  margin-left: 10px;
  margin-right: 20px;
}
</style>